package horncomb.bpm.persistent;

import horncomb.bpm.task.core.TaskInstance;

import java.util.List;

import org.springframework.dao.DataAccessException;

import com.googlecode.horncomb.bean.filter.SimpleMapConditionSet;
import com.googlecode.horncomb.bean.order.DefaultOrder;
import com.googlecode.horncomb.bean.order.DefaultOrderToken;
import com.googlecode.horncomb.bean.paged.PagedList;
import com.googlecode.horncomb.orm.ibatis.SqlMapEntityDaoImpl;

/**
 * DefaultTaskInstanceEntity data access object
 * <p>
 * 负责人/principal:<br/>
 * 修改记录/revision:<br/>
 * 日期:<br/>
 * 修改人:<br/>
 * 修改说明:<br/>
 * @author
 * @version $Id$
 */
public class DefaultTaskInstanceDao extends SqlMapEntityDaoImpl<TaskInstance> {
	private static final long serialVersionUID = 3034156972614232534L;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void insert(TaskInstance entity) throws DataAccessException {
		this.insert(entity.getEntity(), null);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void update(TaskInstance entity) throws DataAccessException {
		this.update(entity.getEntity(), null);
	}

	/**
	 * @param actor
	 * @return
	 */
	public List<? extends TaskInstance> queryActorTodoTaskInstances(String actor) {
		return this.query(new SimpleMapConditionSet().addConstraint(
				"likeActor", actor).addConstraint("isEnd", false), null);
	}

	/**
	 * @param tokenId
	 * @return
	 */
	public List<? extends TaskInstance> queryTodoTaskInstancesInToken(
			String tokenId) {
		return this.query(new SimpleMapConditionSet().addConstraint("tokenId",
				tokenId).addConstraint("isEnd", false), null);
	}

	/**
	 * @param actor
	 * @param isEnd
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public PagedList<List<? extends TaskInstance>> findActorTodoTaskInstances(
			String actor, boolean isEnd, int page, int pageSize) {
		return super.query(new SimpleMapConditionSet().addConstraint(
				"likeActor", actor).addConstraint("isEnd", isEnd),
				new DefaultOrder(new DefaultOrderToken("START_", false)), page,
				pageSize);
	}

	/**
	 * @param tokenId
	 * @return
	 */
	public List<? extends TaskInstance> queryTaskInstancesByTokenId(
			String tokenId) {
		return this.query(new SimpleMapConditionSet().addConstraint("tokenId",
				tokenId), new DefaultOrder(new DefaultOrderToken("CREATE_")));
	}
}
